package org.fengling.readingplatform.novel.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.fengling.readingplatform.model.entity.novel.Chapter;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.fengling.readingplatform.model.vo.novel.ChapterVo;
import org.fengling.readingplatform.novel.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Tag(name = "章节管理")
@RestController
@RequestMapping(value="/api/novel/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @Operation(summary = "查询小说内容的方法")
    @GetMapping(value="/getContent/{novelId}/{chapterId}")
    public Result getContent(@PathVariable("novelId") long novelId,
                             @PathVariable("chapterId") long chapterId) {
        CompletableFuture<Chapter> chapter = chapterService.getContent(novelId, chapterId);
        Chapter chapter1;
        try {
            chapter1 = chapter.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        return Result.build(chapter1, ResultCodeEnum.SUCCESS) ;
    }


    @Operation(summary = "查询所有章节的方法")
    @GetMapping(value="/getChapter/{novelId}")
    public Result getChapter(@PathVariable("novelId") long novelId) {
        List<ChapterVo> list = chapterService.getAllChapter(novelId);
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }
}
